<template>
    <div id="app">
        <router-view></router-view>
    </div>
</template>

<script>
    import storage from './storage'

    export default {
        name: 'App',
        data() {
            return {
                res: {}
            }
        },
        mounted() {
            // console.log(JSON.stringify(storage.getItem('user')))
            // key:mall value:{"user":{"username":"jack","age":30,"sex":1,"birth":"19860923"}}
            // storage.setItem("birth","19860923","user")
            // 添加对象
            // storage.setItem("data",{"code":200,"msg":"success"})
            // 删除(测试用)
            storage.clear("data", "user")

            if (this.$cookie.get('userId')) {
                this.getUser();
                this.getCartCount()
            }
        },
        methods:{
            getUser(){
                this.axios.get('/user').then((res={})=>{
                    // 分发Action
                    this.$store.dispatch('saveUserName',res.username);
                })
            },
            getCartCount() {
                this.axios.get('/carts/products/sum').then((res=0)=>{
                    // 分发Action
                    this.$store.dispatch('saveCartCount',res);
                })
            }
        }
    }
</script>

<style lang="scss">
    @import "./assets/scss/reset.scss";
    @import "./assets/scss/config.scss";
    @import "./assets/scss/button.scss";
</style>
